package timing.ukulele.third.service.weixin;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.weixin.WxAutoReplyDTO;
import timing.ukulele.data.third.view.common.EnumItemVO;
import timing.ukulele.data.third.view.weixin.WxArticleVO;
import timing.ukulele.data.third.view.weixin.WxAutoReplyVO;
import timing.ukulele.third.constant.WxConstant;
import timing.ukulele.third.convertor.weixin.WxAutoReplyConverter;
import timing.ukulele.third.dictionary.wx.WxAutoReqTypeEnum;
import timing.ukulele.third.persistent.weixin.WxAutoReply;
import timing.ukulele.third.mapper.wexin.WxAutoReplyMapper;
import timing.ukulele.third.service.common.BaseOperatorService;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息自动回复
 */
@Service
public class WxAutoReplyService extends BaseOperatorService<WxAutoReplyMapper, WxAutoReply> {

    public IPage<WxAutoReplyVO> pageList(WxAutoReplyDTO wxAutoReplyDTO) {
        Page pageParam = new Page(wxAutoReplyDTO.getCurrent(), wxAutoReplyDTO.getPageSize());
        Page page = this.lambdaQuery()
                .eq(WxAutoReply::getAppId, wxAutoReplyDTO.getAppId())
                .eq(WxAutoReply::getType, wxAutoReplyDTO.getType())
                .page(pageParam);
        List<WxAutoReplyVO> list = new ArrayList<>();
        page.getRecords().forEach(item -> {
            WxAutoReplyVO wxAutoReplyVO = WxAutoReplyConverter.INSTANCE.p2v((WxAutoReply)item);
            wxAutoReplyVO.setArticleList(JSON.parseArray(wxAutoReplyVO.getContent(), WxArticleVO.class));
            list.add(wxAutoReplyVO);
        });
        page.setRecords(list);
        return page;
    }

    public WxAutoReplyVO getDetails(Long id) {
        WxAutoReply wxAutoReply = this.getById(id);
        return WxAutoReplyConverter.INSTANCE.p2v(wxAutoReply);
    }

    public Boolean saveWxAutoReply(WxAutoReplyDTO wxAutoReplyDTO) {
        // 预先验证
        validateWriteOperation(wxAutoReplyDTO);
        // 保存数据
        WxAutoReply wxAutoReply = WxAutoReplyConverter.INSTANCE.d2p(wxAutoReplyDTO);
        return this.save(wxAutoReply);
    }

    public void validateWriteOperation(WxAutoReplyDTO wxAutoReplyDTO) {
        if(WxConstant.WX_AUTO_REPLY_TYPE_2.equals(wxAutoReplyDTO.getType())) {
            if(this.lambdaQuery()
                    .eq(WxAutoReply::getAppId, wxAutoReplyDTO.getAppId())
                    .eq(WxAutoReply::getRepType, wxAutoReplyDTO.getRepType())
                    .notIn(ObjectUtil.isNotNull(wxAutoReplyDTO.getId()), WxAutoReply::getId, wxAutoReplyDTO.getId())
                    .count() > 1) {
                throw new RuntimeException("请求消息类型重复");
            }
        }
        if(WxConstant.WX_AUTO_REPLY_TYPE_3.equals(wxAutoReplyDTO.getType())) {
            if(this.lambdaQuery()
                    .eq(WxAutoReply::getAppId, wxAutoReplyDTO.getAppId())
                    .eq(WxAutoReply::getRepType, wxAutoReplyDTO.getRepType())
                    .eq(WxAutoReply::getReqKey, wxAutoReplyDTO.getReqKey())
                    .notIn(ObjectUtil.isNotNull(wxAutoReplyDTO.getId()), WxAutoReply::getId, wxAutoReplyDTO.getId())
                    .count() > 1) {
                throw new RuntimeException("关键词重复");
            }
        }
        wxAutoReplyDTO.setContent(JSON.toJSONString(wxAutoReplyDTO.getArticleList()));
    }

    public Boolean updateWxAutoReply(WxAutoReplyDTO wxAutoReplyDTO) {
        // 预先验证
        validateWriteOperation(wxAutoReplyDTO);
        // 修改数据
        WxAutoReply wxAutoReply = WxAutoReplyConverter.INSTANCE.d2p(wxAutoReplyDTO);
        return this.updateById(wxAutoReply);
    }

    public Boolean deleteWxAutoReply(Long id) {
        return Boolean.TRUE;
    }

    public List<EnumItemVO> getWxAutoReqType(String appId) {
        WxAutoReqTypeEnum[] wxAutoReqTypeList = WxAutoReqTypeEnum.values();
        List<EnumItemVO> resultList = new ArrayList<>();
        for (WxAutoReqTypeEnum wxAutoReqTypeEnum : wxAutoReqTypeList) {
            EnumItemVO enumItemVO = new EnumItemVO();
            enumItemVO.setValue(wxAutoReqTypeEnum.getValue());
            enumItemVO.setDesc(wxAutoReqTypeEnum.getDesc());
            resultList.add(enumItemVO);
        }
        return resultList;
    }

}
